package com.dlc.felear.xiaoensale.utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;

import com.dlc.felear.xiaoensale.R;

/**
 * 语音提示类
 */
public class PlayVoiceUtil implements Handler.Callback {
    private static final String TAG = "PlayVoiceUtil";
    private static final int WHAT_1 = 33;
    /**
     * 上下文
     */
    private Context context;
    /**
     * 开门提示
     */
    public static final String ERROR = "error";

    /**
     * 实体单例
     */
    private static PlayVoiceUtil instance;
    /**
     * 播放实体类
     */
    private MediaPlayer player;
    private Handler handler;


    public static PlayVoiceUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (PlayVoiceUtil.class) {
                if (instance == null) {
                    instance = new PlayVoiceUtil(context);
                }
            }
        }
        return instance;
    }
    public static PlayVoiceUtil getInstance() {

        return instance;
    }

    private PlayVoiceUtil(Context context) {
        this.context = context;
        handler=new Handler(this);
    }

    private boolean isPlay=false;
    public void playRfidVoice(int flag) {
        if(isPlay){
            return;
        }
        handler.sendEmptyMessageDelayed(WHAT_1,2000);
        isPlay=true;
        switch (flag) {
            case 0:
                //加盖子
                playVioce(R.raw.qjgz);
                break;
            case 1:
                //拿掉盖子
                playVioce(R.raw.nzgz);
                break;
            case 2:
                //支付成功
                playVioce(R.raw.pay_success);
                break;
            case 3:
                //请重新放置餐盘
                 playVioce(R.raw.tips_re_put_plate);
                break;
            case 4:
                //用户未绑定餐盘
                playVioce(R.raw.plate_no_bind);
                break;
            case 5:
                //设备故障请勿使用
                playVioce(R.raw.device_error);
                break;
            case 6:
                //请不要把夹子放进菜盘中
                playVioce(R.raw.tips_position);
                break;
            case 7:
                //余额不足，请充值
                playVioce(R.raw.pay_fail);
                break;
            case 8:
                //放错货道
                playVioce(R.raw.put_error);
                break;
            case 9:
                //操作成功，请拿走餐盘
                playVioce(R.raw.operation_success);
                break;
            case 10:
                // 炒菜完成
                playVioce(R.raw.done_tips);
                break;
            case 11:
                //设备有故障
                playVioce(R.raw.done_tips);
                break;
            case 12:
                //未绑定菜谱
                playVioce(R.raw.no_bind_emun_tips);
                break;
            case 13:
                //加菜语音提示
                playVioce(R.raw.jc_tips);
                break;

            default:
        }
    }


    private void playRingVoice(Uri pickUri) {
        player = new MediaPlayer();
        try {
            player.setDataSource(context, pickUri);
        } catch (Exception e) {
            e.printStackTrace();
        }
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            player.setAudioStreamType(AudioManager.STREAM_ALARM);
            try {
                player.prepare();
            } catch (Exception e) {
                e.printStackTrace();
            }
            player.start();
        }
    }

    /**
     * 播放raw文件夹里面的文件
     *
     * @param reId
     */
    public void playVioce(int reId) {
        Uri uri = Uri.parse("android.resource://com.dlc.felear.xiaoensale/" + reId);
        if (uri != null) {
            playRingVoice(uri);
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        isPlay=false;
        return false;
    }
}
